package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.TalkRoomMapper;
import com.link_with_health.model.TalkRoomEntity;
import com.link_with_health.service.BaseService;
import com.link_with_health.service.TalkRoomService;
import org.apache.ibatis.jdbc.Null;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("talkRoomService")
public class TalkRoomServiceImpl extends BaseService implements TalkRoomService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    TalkRoomMapper talkRoomMapper;

    /**
     * 通过聊天室id获得聊天信息
     * @param id 聊天室id
     * @return 聊天信息
     * @throws BaseException 异常
     */
    @Override
    public TalkRoomEntity getTalkRoomById(Long id) throws BaseException {
        TalkRoomEntity entity = null;
        try {
            entity = talkRoomMapper.queryTalkRoomById(id);
        } catch (Exception e) {
            logger.error("[query talkRoom failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("TALKROOM.ID: "+id);
        return entity;
    }

    /**
     * 通过医生id获取聊天信息
     * @param id 医生id
     * @return 聊天信息
     * @throws BaseException 异常
     */
    @Override
    public List<TalkRoomEntity> getTalkRoomByDoctor(Long id) throws BaseException {
        List<TalkRoomEntity> talkRoomList = null;
        try {
            talkRoomList = talkRoomMapper.queryTalkRoomByDoctor(id);
        } catch (Exception e) {
            logger.error("[query talkRoom failed]",e);
        }

        if (talkRoomList == null || talkRoomList.isEmpty())
            throw new NullQueryResultException("TALKROOM INQUIRY.DR_ID: "+id);
        return talkRoomList;
    }

    /**
     * 通过患者id获取聊天信息
     * @param id 患者id
     * @return 聊天信息
     * @throws BaseException 异常
     */
    @Override
    public List<TalkRoomEntity> getTalkRoomByPatient(Long id) throws BaseException {
        List<TalkRoomEntity> talkRoomList = null;
        try {
            talkRoomList = talkRoomMapper.queryTalkRoomByPatient(id);
        } catch(Exception e) {
            logger.error("[query talkRoom failed]",e);
        }
        if (talkRoomList == null || talkRoomList.isEmpty())
            throw new NullQueryResultException("TALKROOM INQUIRY.PATIENT_ID: "+id);
        return talkRoomList;
    }

    /**
     * 通过问诊id获取聊天信息
     * @param id 问诊信息id
     * @return  聊天信息
     * @throws BaseException 异常
     */
    @Override
    public List<TalkRoomEntity> getTalkRoomByInquiry(Long id) throws BaseException {
        List<TalkRoomEntity> talkRoomList = null;
        try {
            talkRoomList = talkRoomMapper.queryTalkRoomByInquiry(id);
        } catch (Exception e){
            logger.error("[query talkRoom failed]",e);
        }
        if (talkRoomList == null || talkRoomList.isEmpty())
            throw new NullQueryResultException("TALKROOM.INQUIRY_ID: "+id);

        return talkRoomList;
    }

    /**
     * 获取所有聊天信息
     * @return 聊天信息
     * @throws BaseException 异常
     */
    @Override
    public List<TalkRoomEntity> getAllTalkRoom() throws BaseException {
        List<TalkRoomEntity> talkRoomList = null;
        try {
            talkRoomList = talkRoomMapper.queryAllTalkRoom();
        } catch (Exception e) {
            logger.error("[query talkRoom failed]", e);
        }
        if (talkRoomList == null || talkRoomList.isEmpty())
            throw new NullQueryResultException("ALL TALKROOM");
        return talkRoomList;
    }

    /**
     * 修改聊天信息
     * @param talkRoom 聊天信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeTalkRoom(TalkRoomEntity talkRoom) throws BaseException {
        int result = 0;
        try {
            result = talkRoomMapper.updateTalkRoom(talkRoom);

        } catch(Exception e) {
            logger.error("[change talkRoom failed]", e);
        }

        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE TALKROOM: "+talkRoom);
        return result;
    }

    /**
     * 新增聊天信息
     * @param talkRoom 聊天信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addTalkRoom(TalkRoomEntity talkRoom) throws BaseException {
        int result = 0;
        try {
            result = talkRoomMapper.insertTalkRoom(talkRoom);
        } catch (Exception e) {
            logger.error("[add talkRoom failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INSERT TALKROOM: "+talkRoom);
        return result;
    }

    /**
     * 删除聊天信息
     * @param id 聊天信息id
     * @return 改动数量id
     * @throws BaseException 异常
     */
    @Override
    public int deleteTalkRoom(Long id) throws BaseException {
        int result = 0;
        try {
            result = talkRoomMapper.deleteTalkRoom(id);
        } catch (Exception e) {
            logger.error("[delete talkRoom failed]");
        }
        if (result == 0)
            throw  new ModifyDatabaseFailedException("DELETE TALKROOM.ID: " +id);
        return result;
    }
}
